package vip.xiaonuo.dev.modular.job.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
import vip.xiaonuo.dev.modular.job.entity.Tousu;
import vip.xiaonuo.dev.modular.job.mapper.TousuJobMapper;
import vip.xiaonuo.dev.modular.job.util.HttpApiUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 定时器的一个示例
 *
 * @author xuyuxiang
 * @date 2022/8/5 15:52
 **/
@Slf4j
@Component
public class TousuDetailJobTimerTaskRunner implements CommonTimerTaskRunner {

    @Autowired
    TousuJobMapper tousuMapper;

    /**
     * 获取投诉数据逻辑：
     * 1.调用登陆接口，获取cookie
     */
    @Override
    public void action(String extJson) {
        log.info("抓取投诉数据定时任务开始执行。。。");
        // 调用http请求获取网页内容
        String loginUrl = "http://183.247.194.62:63636/login";
        String detailUrl = "http://183.247.194.62:63636/records/";
        Map<String, String> map = tousuMapper.getDhbaAP();
        String username ="";
        String password ="";
        if (map.containsKey("username") && map.containsKey("password")){
            username = map.get("username");
            password = map.get("password");
        }
        if(username.isEmpty()){
            username = "a15558558858";
            password = "Aa123456";
        }
        String cookie = HttpApiUtil.getTousuLogin(loginUrl,username,password);
        // 获取需要查询的投诉列表
        List<Tousu> tousuList = tousuMapper.selectToDealTousu();
//        List<Tousu> tousuList = new ArrayList<>();
//        Tousu tousu1 = new Tousu();
//        tousu1.setComplaintId("UEtONVNJRGNJOVlrTk9QNGlNaFVCQT09");
//        tousuList.add(tousu1);
        ExecutorService executor = Executors.newFixedThreadPool(5);
        // 循环根据id获取detail
        for (Tousu tousu : tousuList) {
            executor.execute(() -> {
                String id = tousu.getComplaintId();
                JSONObject detail = HttpApiUtil.getTousuDetail(detailUrl + id, cookie);
                // 将detail更新到数据库
                if (detail != null && !detail.isEmpty()) {
                    // 投诉内容
                    tousu.setComplaintContent(detail.getString("complaint_content"));
                    // 投诉人号码
                    tousu.setAccusedNumber(detail.getString("complaint_number"));
                    String complained_province = detail.getString("complained_province");
                    String complained_operator = detail.getString("complained_operator");
                    if (complained_province != null && !complained_province.isEmpty() &&
                            complained_operator!= null && !complained_operator.isEmpty()) {
                        tousu.setAccusedProvince(complained_province);
                        tousu.setAccusedOperator(complained_operator);
                        tousu.setBelongTime(DateUtil.date());
                    }
                    tousuMapper.updateTousuDetail(tousu);
                }
            });
        }
        executor.shutdown();
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            log.error("线程池等待被中断", e);
        }
    }
}
